package ssm.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import ssm.domain.Member;
import ssm.domain.Orders;
import ssm.domain.Product;
import ssm.domain.Traveller;

import java.util.List;

@Repository
public interface IOrdersDao {

    @Select("select * from orders")
    /*由于订单中不仅含有订单人的信息，还同时包括订购的product信息，因此还需要查orders表；
    * 同时，使用结果集进行封装*/
    @Results({
        @Result(id = true,column = "id",property = "id"),
        @Result(column = "orderNum",property = "orderNum"),
        @Result(column = "orderTime",property = "orderTime"),
        @Result(column = "orderStatus",property = "orderStatus"),
        @Result(column = "peopleCount",property = "peopleCount"),
        @Result(column = "payType",property = "payType"),
        @Result(column = "orderDesc",property = "orderDesc"),

            /*对于Product类的查询与封装，通过one与javatype完成。
            * 查询语句，直接指定查询语句的所在路径：某个包+接口名+方法名*/
        @Result(column = "productId",property = "product",javaType = Product.class,one = @One(select = "ssm.dao.IProductDao.findById"))
    }
    )
   public List<Orders> findAll() throws Exception;


    @Select("select * from orders where id = #{ordersId}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "orderNum",property = "orderNum"),
            @Result(column = "orderTime",property = "orderTime"),
            @Result(column = "orderStatus",property = "orderStatus"),
            @Result(column = "peopleCount",property = "peopleCount"),
            @Result(column = "payType",property = "payType"),
            @Result(column = "orderDesc",property = "orderDesc"),

            /*对于Product类的查询与封装，通过one与javatype完成。
             * 查询语句，直接指定查询语句的所在路径：某个包+接口名+方法名*/
            @Result(column = "productId",property = "product",javaType = Product.class,one = @One(select = "ssm.dao.IProductDao.findById")),
            @Result(column = "memberId",property = "member",javaType = Member.class,one = @One(select = "ssm.dao.IMemberDao.findById")),

            /*此处为订单与旅客之间的多对多查询*/
            @Result(column = "id",property = "travellers",javaType = java.util.List.class,many=@Many(select = "ssm.dao.ITravellerDao.findByOrdersId"))
    })
    public Orders findById(String ordersId) throws  Exception;
}
